package domain

import (
	"context"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
	"grpc-common/search/types/search"
	"listenboos-common/kafka/model"
	"search/dao"
	"search/repo"
)

type SearchDomain struct {
	SearchRepo repo.SearchAlbumRepo
	suggest    repo.SearchSuggestRepo
}

func (d *SearchDomain) Search(ctx context.Context, keyWords []string) ([]*search.SearchAlbumInfo, error) {
	albums, err := d.SearchRepo.Search(ctx, keyWords)
	if err != nil {
		logx.Errorf("elasticsearch内部错误%v", err)
		return nil, err
	}
	var info []*search.SearchAlbumInfo
	copier.Copy(&info, &albums)
	return info, nil
}
func (d *SearchDomain) SearchChannel(ctx context.Context, keyWords []string) ([]*search.SearchAlbumInfo, error) {
	albums, err := d.SearchRepo.SearchChannel(ctx, keyWords)
	if err != nil {
		logx.Errorf("elasticsearch内部错误%v", err)
		return nil, err
	}
	var info []*search.SearchAlbumInfo
	copier.Copy(&info, &albums)
	return info, nil
}
func (d *SearchDomain) OnOnSaleAlbum(ctx context.Context, data model.AlbumInfo) error {
	err := d.SearchRepo.InputAlbum(ctx, data)
	if err != nil {
		logx.Errorf("elasticsearch内部错误%v", err)
		return err
	}
	return nil
}

func (d *SearchDomain) SearchAlbumInfo(ctx context.Context, req *search.AlbumInfoReq) ([]*search.AlbumInfo, int64, int64, error) {
	albums, total, totalPages, err := d.SearchRepo.SearchAlbumInfo(ctx, req, req.PageNo, req.PageSize)
	if err != nil {
		logx.Errorf("elasticsearch内部错误%v", err)
		return nil, 0, 0, err
	}
	var info []*search.AlbumInfo
	err = copier.Copy(&info, &albums)
	if err != nil {
		logx.Errorf("elasticsearch内部错误%v", err)
		return nil, 0, 0, err
	}
	return info, total, totalPages, nil
}

// TODO:未完成 dao层还未实现
func (d *SearchDomain) KeyWord(ctx context.Context, keyWords []string) (*search.KeywordResp, error) {
	keys, err := d.SearchRepo.KeyWord(ctx, keyWords)
	if err != nil {
		logx.Errorf("elasticsearch内部错误%v", err)
		return nil, err
	}
	var info *search.KeywordResp
	copier.Copy(&info.Keywords, &keys)
	return info, nil
}
func (d *SearchDomain) SuggestSearch(ctx context.Context, sug string) (res []string, err error) {
	keys, err := d.suggest.SuggestSearch(ctx, sug)
	if err != nil {
		logx.Errorf("elasticsearch内部错误%v", err)
		return nil, err
	}
	return keys, nil
}
func (d *SearchDomain) UpdateRanking(ctx context.Context, category1Id int64, dimension string) ([]model.AlbumInfo, error) {
	keys, err := d.SearchRepo.UpdateRanking(ctx, category1Id, dimension)
	if err != nil {
		logx.Errorf("elasticsearch内部错误%v", err)
		return nil, err
	}
	return keys, nil
}

func (d *SearchDomain) FindAlbumInfo(ctx context.Context, keyWords string) (*search.AlbumInfo, error) {
	albums, err := d.SearchRepo.FindAlbumInfo(ctx, keyWords)
	if err != nil {
		logx.Errorf("elasticsearch内部错误%v", err)
		return nil, err
	}
	info := search.AlbumInfo{}
	err = copier.Copy(&info, &albums)
	if err != nil {
		return nil, err
	}
	return &info, nil
}
func NewSearchDomain() *SearchDomain {
	return &SearchDomain{
		SearchRepo: dao.NewAlbumElasticDAO(),
		suggest:    dao.NewSuggestElasticDAO(),
	}
}
